package com.hnqcgc.dao.impl;

import com.hnqcgc.dao.BaseDao;
import com.hnqcgc.dao.ICompanyDao;
import com.hnqcgc.db.MyTransaction;
import com.hnqcgc.vo.TAdmin;
import com.hnqcgc.vo.TCompany;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CompanyDaolmpl extends BaseDao implements ICompanyDao {

    private MyTransaction myTransaction;
    public CompanyDaolmpl(MyTransaction myTransaction){
        super(myTransaction);
        this.myTransaction=myTransaction;
    }
    @Override
    public List<TCompany> queryAll() throws SQLException {
        String sql="select * from t_company";
        List<TAdmin> list=new ArrayList<>();
        QueryRunner queryRunner=new QueryRunner();
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);
        return  queryRunner.query(myTransaction.getConn(),sql,new BeanListHandler<TCompany>(TCompany.class,rowProcessor));
    }

    @Override
    public List<TCompany> queryByPage(int pagesize, int pagenum) throws SQLException {
        String sql = "select * from t_company limit ?,?";
        QueryRunner queryRunner = new QueryRunner();
        GenerousBeanProcessor beanProcessor = new GenerousBeanProcessor();
        RowProcessor rowProcessor = new BasicRowProcessor(beanProcessor);


            return queryRunner.query(myTransaction.getConn(), sql, new BeanListHandler<TCompany>(TCompany.class, rowProcessor), (pagenum - 1) * pagesize, pagesize);
        }

        }


